# coding: utf-8
from PySide6.QtWidgets import QWidget, QVBoxLayout, QStackedWidget
from qfluentwidgets import SegmentedWidget

from common import signalBus
from server.api import LenovoAPI as Lenovo
from .classic_interface import ClassicInterface
from .interface import LenovoBaseInterface
from ..base import WallpaperInterface


class LenovoBannerInterface(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.verticalLayout = QVBoxLayout(self)
        self.pivot = SegmentedWidget(self)
        self.stackedWidget = QStackedWidget(self)
        self.wallpaperInterface = LenovoBaseInterface(self)
        self.wallpaperHotInterface = LenovoBaseInterface(self)
        self.wallpaperRankInterface = LenovoBaseInterface(self)
        self.classicInterface = ClassicInterface(self)
        self.items = {
            'wallpaper': (self.wallpaperInterface, '精选'),
            'wallpaperHot': (self.wallpaperHotInterface, '热门'),
            'wallpaperRank': (self.wallpaperRankInterface, '排行'),
            'classic': (self.classicInterface, '分类')
        }

        self.verticalLayout.setContentsMargins(10, 10, 0, 0)
        self.verticalLayout.addWidget(self.pivot)
        self.verticalLayout.addWidget(self.stackedWidget)

        self.pivot.currentItemChanged.connect(self.switchTo)
        for key, value in self.items.items():
            obj, text = value
            self.addSubInterface(obj, key, text)

    def loadInitData(self):
        self.pivot.setCurrentItem('wallpaper')

    def addSubInterface(self, widget: QWidget, routeKey: str, text: str):
        self.stackedWidget.addWidget(widget)
        self.pivot.addItem(routeKey, text)

    def switchTo(self, routeKey: str):
        def load_slot(ob, routeKey: str):
            ob.load(Lenovo.banner, routeKey, slot=ob.onBanner)
            ob.load(Lenovo.group_content, routeKey, slot=ob.onGroupContent)

        def clear_slot(ob, routeKey: str):
            ob.clear()
            load_slot(ob, routeKey)

        obj, _ = self.items[routeKey]
        if not obj.isLoaded():
            if routeKey != 'classic':
                load_slot(obj, routeKey)
                obj.reloadSignal.connect(lambda o=obj, key=routeKey: clear_slot(o, key))
            else:
                obj.initLoadData()
            obj.setLoaded(True)

        self.stackedWidget.setCurrentWidget(obj)


class LenovoInterface(QStackedWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.bannerInterface = LenovoBannerInterface(self)
        self.wallpaperInterface = WallpaperInterface(self)

        self.setObjectName("LenovoInterface")
        self.addWidget(self.bannerInterface)
        self.addWidget(self.wallpaperInterface)

        signalBus.lenovoRankMoreSignal.connect(self.onRankMore)
        self.wallpaperInterface.returnSignal.connect(lambda: self.setCurrentWidget(self.bannerInterface))

    def loadInitData(self):
        self.bannerInterface.loadInitData()

    def onRankMore(self, rank_id: int):
        self.wallpaperInterface.clear()
        self.wallpaperInterface.verticalScrollBar().setValue(0)
        self.wallpaperInterface.load(Lenovo.rank_more, rank_id, slot=self.onWallpaperRankMore)
        self.setCurrentWidget(self.wallpaperInterface)

    def onWallpaperRankMore(self, result: dict):
        self.wallpaperInterface.clear()
        data = result.get('data')
        name = data.get('name')
        desc = data.get('desc')
        self.wallpaperInterface.setTitle(name)
        self.wallpaperInterface.setDescription(desc)
        wallpapers = data.get('wallpapers', [])
        for wallpaper in wallpapers:
            wallpaperImg = wallpaper.get('wallpaperImg')[0]
            uri = wallpaperImg['uri']
            max_url = wallpaper.get('wallpaperImg')[-1]['uri']
            imgRemark = wallpaperImg.get('imgRemark')

            label = self.wallpaperInterface.addLabel(uri)
            label.setMaxUrl(max_url)
            label.setTitle(imgRemark)
        self.wallpaperInterface.lazyTimerStart()
